<template>
  <el-dialog
      title="添加/修改设备"
      :visible.sync="visible"
      :id.sync="id"
      @opened="openDialog"
      :before-close="closeDialog"
      width="480px">
    <el-form label-width="80px">
      <el-form-item label="教室名称" prop="classroom.classroomName" size="small">
        <el-input size="small" v-model="classroom.classroomName"></el-input>
      </el-form-item>
      <el-form-item label="教室编号" prop="classroom.classroomCode" size="small">
        <el-input size="small" v-model="classroom.classroomCode"></el-input>
      </el-form-item>
      <el-form-item label="教室容量" prop="classroom.capacity" size="small">
        <el-input size="small" v-model="classroom.capacity"></el-input>
      </el-form-item>
      <el-form-item label="所在位置" prop="classroom.location" size="small">
        <el-select v-model="classroom.location">
          <el-option
              v-for="item in location"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在楼层" prop="classroom.floor" size="small">
        <el-select v-model="classroom.floor">
          <el-option
              v-for="item in floor"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="使用模式" prop="classroom.usageMode" size="small">
        <el-select size="small" v-model="classroom.usageMode">
        <el-option
            v-for="item in usageMode"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="classroom.usageStatus" size="small">
        <el-select size="small" v-model="classroom.usageStatus">
        <el-option
            v-for="item in usageStatus"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="center" style="margin-left: -80px;">
        <el-button type="primary" size="small" plain @click="handleUpdate" v-if="id!=''">修改设备</el-button>
        <el-button type="primary" size="  small" plain @click="onSubmit" v-else>添加设备</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getClassroomById, updateClassroom, createClassroom} from "@/api/classroom";

export default {
  name: 'ClassroomListEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      floor: [
        {id: 1, name: '1层'},
        {id: 2, name: '2层'},
        {id: 3, name: '3层'},
        {id: 4, name: '4层'},
        {id: 5, name: '5层'}
      ],
      location: [
        {id: 1, name: '笃行楼'},
        {id: 2, name: '知行楼'},
        {id: 3, name: '敏行楼'},
        {id: 4, name: '躬行楼'},

      ],
      usageMode:[
        {id: 1, name: '课堂模式'},
        {id: 2, name: '活动室模式'},
      ],
      usageStatus:[
        {id: 1, name: '空闲'},
        {id: 2, name: '进行中'},
        {id: 3, name: '关闭'},
        {id: 4, name: '故障'},
      ],
      classroom: {}
    }
  },
  methods: {

    // 添加设备信息
    onSubmit() {
      createClassroom({
        'classroomName': this.classroom.classroomName,
        'classroomCode': this.classroom.classroomCode,
        'floor': this.classroom.floor,
        'location': this.classroom.location,
        'capacity':this.classroom.capacity,
        'usageMode': this.classroom.usageMode,
        'usageStatus': this.classroom.usageStatus,
        'createTime':this.classroom.createTime,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 更新设备信息
    handleUpdate() {
      console.log(this.classroom)
      updateClassroom({
        'id': this.classroom.id,
        'classroomName': this.classroom.classroomName,
        'classroomCode': this.classroom.classroomCode,
        'floor': this.classroom.floor,
        'location': this.classroom.location,
        'capacity':this.classroom.capacity,
        'usageMode': this.classroom.usageMode,
        'usageStatus': this.classroom.usageStatus,
        'createTime':this.classroom.createTime,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 打开对话框
    openDialog() {
      if (this.id !== '') {
        getClassroomById(this.id).then((response) => {
          this.classroom = response.data.data;
          // this.classroom.createTime;
        })
      }
    },

    // 关闭对话框
    closeDialog() {
      this.classroom = {}
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>

</style>
